package com.pds.web.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.area.entity.PdsArea;
import com.pds.web.area.mapper.PdsAreaMapper;
import com.pds.web.area.service.PdsAreaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 行政地区表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-19
 */
@Service("pdsAreaService")
public class PdsAreaServiceImpl extends ServiceImpl<PdsAreaMapper, PdsArea> implements PdsAreaService {
    @Resource
    private  PdsAreaMapper pdsAreaMapper;

    @Override
    public List<PdsArea> selectList(PdsArea pdsArea) {
        QueryWrapper<PdsArea> wrapper = new QueryWrapper<>();
        String parentCode = pdsArea.getParentCode();
        String level = pdsArea.getLevel();
        if(!Objects.isNull(parentCode) && !parentCode.isEmpty()){
            wrapper.eq("parent_code",parentCode);
        }
        if(!Objects.isNull(level) && !level.isEmpty()){
            wrapper.eq("level",level);
        }
        return pdsAreaMapper.selectList(wrapper);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsArea insert(PdsArea pdsArea) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsArea.setCreateTime(date);
        pdsAreaMapper.insert(pdsArea);
        return pdsArea;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsAreaMapper.deleteById(id) > 0;
    }
}